//
//  LXNearByViewController.m
//  跑跑快销
//
//  Created by linxian on 14-4-30.
//  Copyright (c) 2014年 ylife. All rights reserved.
//
#define nameLabelWidth                  32
#import "LXNearByViewController.h"

@interface LXNearByViewController ()

@end

@implementation LXNearByViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"周边";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    LXNearByView *nearByView = [[LXNearByView alloc] init];
    self.view = nearByView;
    _nearByView = nearByView;
    
    [self addBarButtons];
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.hidden = YES;
    [self initArray];
    [self initUpView];
}

- (void)initArray
{
    NSArray *iArray = [NSArray arrayWithObjects:@"wrong.png",@"wrong.png",@"wrong.png",@"wrong.png",@"wrong.png",@"wrong.png",@"wrong.png",nil];
    _iArray = iArray;
    NSArray *industryArray = [NSArray arrayWithObjects:@"商场",@"百货",@"超市",@"便利店",@"餐饮",@"五金",@"其他",nil];
    _industryArray = industryArray;
    NSMutableArray *selectArray = [NSMutableArray arrayWithObjects:@"1",@"0",@"0",@"0",@"0",@"0",@"0", nil];
    _selectArray = selectArray;
}

#pragma mark - 初始化asTableView

- (void)initUpView
{
    _nearByView.nbTableView.dataSource = self;
    _nearByView.nbTableView.delegate = self;
    
    [_nearByView.addNewClient addTarget:self action:@selector(down:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - 创建navigationbar上的Item

- (void)addBarButtons
{
    
    // “搜索”按钮
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,50,30)];
    [leftButton setTitle:@"筛选" forState:UIControlStateNormal];
    [leftButton setTitle:@" " forState:UIControlStateHighlighted];
    [leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //    [leftButton setImage:[UIImage imageNamed:@"a.png"] forState: UIControlStateNormal];
    //    [leftButton setImage:[UIImage imageNamed:@"a.png"] forState: UIControlStateHighlighted];
    leftButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [leftButton addTarget:self action:@selector(down:)forControlEvents:UIControlEventTouchUpInside];
    leftButton.tag = 2;
    UIBarButtonItem * fliterButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    
    
    // “周边”按钮
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,50,30)];
    [rightButton setTitle:@"地图" forState:UIControlStateNormal];
    [rightButton setTitle:@" " forState:UIControlStateHighlighted];
    [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //    [rightButton setImage:[UIImage imageNamed:@"a.png"] forState: UIControlStateNormal];
    //    [rightButton setImage:[UIImage imageNamed:@"a.png"] forState: UIControlStateHighligh ted];
    rightButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [rightButton addTarget:self action:@selector(down:)forControlEvents:UIControlEventTouchUpInside];
    rightButton.tag = 3;
    UIBarButtonItem * mapButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    NSArray *btns = [NSArray arrayWithObjects:mapButton, fliterButton, nil];
    [self.navigationItem setRightBarButtonItems:btns];
    
}

- (void)down:(UIBarButtonItem *)sender
{
    if (sender.tag == 1) {
        // 新增客户
        NSLog(@"新增客户");
        LXAddClientViewController * acVC = [[LXAddClientViewController alloc] init];
        [self.navigationController pushViewController:acVC animated:YES];
        
    } else if (sender.tag == 2) {
        // 筛选
        NSLog(@"筛选");
        LXCustomActionSheet *lxCAS = [[LXCustomActionSheet alloc] initWithTitle:@"选择网点类型" withIconArray:_iArray withMsgArray:_industryArray withBtnTitle:@"确定" withSelectArray:_selectArray];
        lxCAS.delegate = self;
        [lxCAS showInView:_nearByView];
        
    } else {
        // 地图
        NSLog(@"地图");
        _showMap = !_showMap;
        if (_showMap) {
            [_nearByView showUpView2];
        } else {
            [_nearByView showUpView1];
        }
    }
}


#pragma mark - tableView的代理方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 8;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70.0f;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    LXNearByTableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[LXNearByTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
    }
    // 标题
    cell.nameLabel.text = @"大润发";
    CGSize sizeName = [cell.nameLabel.text sizeWithAFont:[UIFont systemFontOfSize:16]];
    [cell.nameLabel setFrame:CGRectMake(45, 5, sizeName.width + nameLabelWidth, 35)];
    
    //五角星
    cell.starImgView.frame = CGRectMake(sizeName.width + nameLabelWidth/4, 10, 16, 16);
    cell.starImgView.hidden = NO;
    
    // 地址
    NSString *address = @"地址：";
    cell.addressLabel.text = [address stringByAppendingString:@"苏州昆山市浦东软件园"];
    CGSize sizeAddress = [cell.addressLabel.text sizeWithAFont:[UIFont systemFontOfSize:13]];
    [cell.addressLabel setFrame:CGRectMake(5, 41, sizeAddress.width, 25)];
    
    // 签到图片
    cell.meterLabel.text = @"110m";
    CGSize sizeMeter = [cell.meterLabel.text sizeWithAFont:[UIFont systemFontOfSize:13]];
    [cell.meterLabel setFrame:CGRectMake(270, 10, sizeMeter.width, 28)];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
//    LXClientDetailViewController *detailVC = [[LXClientDetailViewController alloc] init];
//    [self.navigationController pushViewController:detailVC animated:YES];
    
}

#pragma mark - actionsheet的代理
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    // 按钮后处理在这
    LXCustomActionSheet *asheet = (LXCustomActionSheet *)actionSheet;
    if (buttonIndex >= 1 && buttonIndex <= asheet.messageArray.count) {
        
        if ([_selectArray[buttonIndex-1] isEqualToString:@"1"]) {
            _selectArray[buttonIndex-1] = @"0";
        } else {
            _selectArray[buttonIndex-1] = @"1";
        }
        
    } else if (buttonIndex == asheet.messageArray.count+1) {
        // 筛选操作
        for (int i=0; i<asheet.messageArray.count; i++) {
            if ([asheet.selectedArray[i] isEqualToString:@"1"]) {
                NSString *str = asheet.messageArray[i];
                NSLog(@"%@",str);
            }
        }
        
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
